/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.FarmBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 *
 * @author amkreuz
 */
public class FarmUserDAO {

    
    private static Connection DBConn;

    public void init() throws SQLException {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException e) {
            System.err.println(e.getMessage());
            System.exit(0);
        }
        DBConn = DriverManager.getConnection("jdbc:derby://localhost:1527/FarmDB");
    }

    public void terminate() throws SQLException {
        DBConn.close();
    }
    
    public static int createUser(FarmBean bean) throws SQLException {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException e) {
            System.err.println(e.getMessage());
            System.exit(0);
        }

        int rowCount = 0;
        try {
            String myDB = "jdbc:derby://localhost:1527/FarmDB";
            Connection DBConn = DriverManager.getConnection(myDB);
            String insertString;
            Statement stmt = DBConn.createStatement();
            insertString = "INSERT INTO person VALUES ('"
                    + bean.getEmail()
                    + "','" + bean.getPassword()
                    + "','" + bean.isIsAdmin()
                    + "')";

            rowCount = stmt.executeUpdate(insertString);
            System.out.println("insert string = " + insertString);
            DBConn.close();
        } 
        catch (SQLException e) 
        {
            int goof = e.getErrorCode();
            if (goof == 40000) // Cannot Connect
                System.out.println("Cannot connect to DB");
            else if (goof == -1) // Duplicate Username
                System.out.println("Duplicate Username");
            System.err.println(e.getMessage());
        }

        // if insert is successful, rowCount will be set to 1 (1 row inserted successfully). Else, insert failed.
        return rowCount;
    }
    
    public int signInUser(FarmBean bean) throws SQLException {
        
        // This integer will signify how the execution went.
        int executeCode; 
        
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        }
        catch (ClassNotFoundException e) 
        {
            System.err.println(e.getMessage());
            executeCode = -2; // Error Code -2 means Class was not found.
            return executeCode;
        }

        try {
            String myDB = "jdbc:derby://localhost:1527/FarmDB";
            Connection DBConn = DriverManager.getConnection(myDB);
            String insertString;
            Statement stmt = DBConn.createStatement();
            insertString = "SELECT * FROM person "
                    + "WHERE email='" + bean.getEmail()
                    + "' AND password='" + bean.getPassword()
                    + "'";

            System.out.println("select string = " + insertString);
            ResultSet results = stmt.executeQuery(insertString);
            if (results.next() == true)
            {
                executeCode = 2;
            }
            else
            {
                executeCode = 1;
            }
            DBConn.close();
        } 
        catch (SQLException e) 
        {
            System.err.println(e.getMessage());
            executeCode = -1; // Error code -1 means that you could not connect to the DB.
        }

        // if insert is successful, rowCount will be set to 1 (1 row inserted successfully). Else, insert failed.
        return executeCode;
    }
    
    public boolean isAvailable(String user) throws SQLException
    {
        String selectString;
        Statement stmt = DBConn.createStatement();
        selectString = "SELECT * FROM person "
                + "WHERE email=\'" + user + "\'";
        ResultSet rs = stmt.executeQuery(selectString);
        if (rs.next())
            return false;
        else
            return true;
    }
}
